package org.greatlogic.gwtfilereaderwidget.client.itunes;

import com.google.gwt.xml.client.Element;
import java.util.TreeMap;
import org.greatlogic.gwtfilereaderwidget.client.itunes.ITunesEnums.ETrackKeyType;
/**
 * Represents a single track.
 */
public class Track implements IHasKeyAndValueEntries, Comparable<Track> {
//--------------------------------------------------------------------------------------------------
private int                                 _id;
private TreeMap<ETrackKeyType, KeyAndValueEntry> _keyAndValueEntryMap;
//--------------------------------------------------------------------------------------------------
/**
 * @param trackDictElement The "dict" element for a track.
 */
Track(final Element trackDictElement) throws ITunesException {
  _keyAndValueEntryMap = new TreeMap<ITunesEnums.ETrackKeyType, KeyAndValueEntry>();
  KeyAndValueEntry.createFromKeyAndValueParentNode(this, trackDictElement);
  _id = getKeyAndValueEntry(ETrackKeyType.TrackID).getValueAsInt();
} // Track()
//--------------------------------------------------------------------------------------------------
@Override
public void addKeyAndValueEntry(KeyAndValueEntry keyAndValueEntry) {
  _keyAndValueEntryMap.put(keyAndValueEntry.getKeyType(), keyAndValueEntry);
} // addKeyAndValue()
//--------------------------------------------------------------------------------------------------
public boolean asBoolean(final ETrackKeyType keyType) {
  KeyAndValueEntry keyAndValueEntry = _keyAndValueEntryMap.get(keyType);
  return keyAndValueEntry == null ? false : keyAndValueEntry.getValueAsBoolean();
} // asBoolean()
//--------------------------------------------------------------------------------------------------
public int asInt(final ETrackKeyType keyType) {
  KeyAndValueEntry keyAndValueEntry = _keyAndValueEntryMap.get(keyType);
  return keyAndValueEntry == null ? 0 : keyAndValueEntry.getValueAsInt();
} // asInt()
//--------------------------------------------------------------------------------------------------
public long asLong(final ETrackKeyType keyType) {
  KeyAndValueEntry keyAndValueEntry = _keyAndValueEntryMap.get(keyType);
  return keyAndValueEntry == null ? 0 : keyAndValueEntry.getValueAsLong();
} // asLong()
//--------------------------------------------------------------------------------------------------
public String asString(final ETrackKeyType keyType) {
  KeyAndValueEntry keyAndValueEntry = _keyAndValueEntryMap.get(keyType);
  return keyAndValueEntry == null ? "" : keyAndValueEntry.getValueAsString();
} // asString()
//--------------------------------------------------------------------------------------------------
@Override
public int compareTo(final Track otherTrack) {
  return _id - otherTrack._id;
} // compareTo()
//--------------------------------------------------------------------------------------------------
@Override
public KeyAndValueEntry getKeyAndValueEntry(final ETrackKeyType keyType) {
  KeyAndValueEntry result = _keyAndValueEntryMap.get(keyType);
  return result == null ? KeyAndValueEntry.EmptyKeyAndValueEntry : result;
} // getKeyAndValueEntry()
//--------------------------------------------------------------------------------------------------
@Override
public TreeMap<ETrackKeyType, KeyAndValueEntry> getKeyAndValueEntryMap() {
  return _keyAndValueEntryMap;
} // getKeyAndValueEntryMap()
//--------------------------------------------------------------------------------------------------
public int getTrackID() {
  return _id;
} // getTrackID()
//--------------------------------------------------------------------------------------------------
public StringBuilder getXML(final StringBuilder sb, final String xmlIndent,
                            final String endOfLineString) {
  StringBuilder result;
  if (sb == null) {
    result = new StringBuilder(1000);
  }
  else {
    result = sb;
    result.setLength(0);
  }
  String xmlIndent2 = xmlIndent + xmlIndent;
  String xmlIndent3 = xmlIndent2 + xmlIndent;
  result.append(xmlIndent2).append("<key>").append(_id).append("</key>").append(endOfLineString);
  result.append(xmlIndent2).append("<dict>").append(endOfLineString);
  for (KeyAndValueEntry keyAndValueEntry : _keyAndValueEntryMap.values()) {
    result.append(xmlIndent3).append(keyAndValueEntry.getXML()).append(endOfLineString);
  }
  result.append(xmlIndent2).append("</dict>").append(endOfLineString);
  return result;
} // getXML()
//--------------------------------------------------------------------------------------------------
@Override
public boolean processArray(final ETrackKeyType keyType, final Element arrayElement) {
  return false;
} // processArray()
//--------------------------------------------------------------------------------------------------
@Override
public String toString() {
  return "Track ID:" + _id + " " + _keyAndValueEntryMap;
} // toString()
//--------------------------------------------------------------------------------------------------
}